package com.fpj.sat.vehicle_administrator.dao.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.fpj.sat.vehicle_administrator.dao.UsuarioDao;
import com.fpj.sat.vehicle_administrator.domain.Usuario;

@Repository
public class UsuarioDaoImpl implements UsuarioDao {

	private EntityManager em;
	
	@PersistenceContext
	public void setEntityManager(EntityManager em){
		this.em = em;
	}
	
	@Transactional(readOnly=true)
	public Usuario getUsuario(String userName, String password) {
		StringBuilder JPQL = new StringBuilder(); 
		JPQL.append("select u from com.fpj.sat.vehicle_administrator.domain.Usuario u where u.username = :username and u.password = :password ");
		TypedQuery<Usuario> query = em.createQuery(JPQL.toString(), Usuario.class);
		query.setParameter("username", userName);
		query.setParameter("password", password);
		
		return query.getSingleResult();
	}

}
